package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.entity.User;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService service;

    @RequestMapping("/list")
    public R<List<ShoppingCart>> getList(HttpSession session){
        //获取当前登录用户
        User userLogin=(User) session.getAttribute("userLogin");
        //根据当前用户id查询所属购物车列表
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper= new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userLogin.getId());
        shoppingCartLambdaQueryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list=service.list(shoppingCartLambdaQueryWrapper);
        return R.success(list);
    }

    @PostMapping("/add")
    public R<ShoppingCart> addShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        User userLogin=(User) session.getAttribute("userLogin");
        ShoppingCart shoppingCart1=service.addShoppingCart(shoppingCart, userLogin.getId());
        return R.success(shoppingCart1);
    }

    @PostMapping("/sub")
    public R<ShoppingCart> subShoppingCart(@RequestBody ShoppingCart shoppingCart, HttpSession session){
        User userLogin=(User) session.getAttribute("userLogin");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getDishId() != null,ShoppingCart::getDishId,shoppingCart.getDishId());
        shoppingCartLambdaQueryWrapper.eq(shoppingCart.getSetmealId() != null,ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userLogin.getId());
        ShoppingCart dishOrSetmeal=service.getOne(shoppingCartLambdaQueryWrapper);
        //获取到该用户购物车的数据 判断数量如果为1 则删除 不为1 则数量-1
        if(dishOrSetmeal.getNumber() == 1){
            service.removeById(dishOrSetmeal.getId());
            return R.success(null);
        }
        //数量大于1 则数量-1
        dishOrSetmeal.setNumber(dishOrSetmeal.getNumber()-1);
        boolean flag=service.updateById(dishOrSetmeal);

        return flag ? R.success(dishOrSetmeal): R.error("更改失败!");
    }


    @DeleteMapping("/clean")
    public R<String> clean(HttpSession session){
        User userLogin=(User) session.getAttribute("userLogin");
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,userLogin.getId());
        //根据当前登录用户 清除购物车
        service.remove(shoppingCartLambdaQueryWrapper);

        return R.success("清空购物车成功");
    }
}
